package com.csw.shuanfa.CodeImprove.CompleteFutureLinkedBlockingQueue;

import com.csw.shuanfa.utils.ThreadPoolUtil;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author chengshuaiwei
 */
public class CompletableFutureTestShiZhan {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1、
        ExecutorService threadPool = ThreadPoolUtil.getExecutorServiceCPU();

        long t1 = System.currentTimeMillis();
        AtomicInteger a = new AtomicInteger();

        try {
            CompletableFuture<Long> future1 = CompletableFuture.supplyAsync(() -> {
                return doSomthing(a);
            }, threadPool);
            CompletableFuture<Long> future2 = CompletableFuture.supplyAsync(() -> {
                return doSomthing(a);
            }, threadPool);
            CompletableFuture<Long> future3 = CompletableFuture.supplyAsync(() -> {
                try {
                    int i = 1 / 0;
                } catch (Exception e) {
                    System.out.println(Thread.currentThread().getName());
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
                return doSomthing(a);
            }, threadPool);

            //2、
            long time1 = future1.get(1, TimeUnit.SECONDS);
            long time2 = future2.get();
            long time3 = future3.get();

            long t2 = System.currentTimeMillis();
            System.out.println("总共耗时：" + (t2 - t1));
        } catch (Exception e) {
            //3、
            System.out.println(ThreadPoolUtil.getThreadPoolNameMethod());
            threadPool.shutdown();

            throw new RuntimeException(e);
        }

    }

    private static long doSomthing(AtomicInteger a) {
        a.getAndIncrement();
        try {
            TimeUnit.SECONDS.sleep(2);
            //Thread.currentThread().getName());
        } catch (Exception e) {
            e.getMessage();
        }
        return System.currentTimeMillis();
    }


//    /**
//     *
//     * @throws Exception
//     * 无返回值
//     */
//    public static void runAsync() throws Exception {
//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//            try {
//                TimeUnit.SECONDS.sleep(1);
//            } catch (InterruptedException e) {
//            }
//            //run end ...");
//        });
//
//        future.get();
//    }
//
//    /**
//     *
//     * @throws Exception
//     * 有返回值
//     */
//    public static void supplyAsync() throws Exception {
//        CompletableFuture<Long> future = CompletableFuture.supplyAsync(() -> {
//            try {
//                TimeUnit.SECONDS.sleep(1);
//            } catch (InterruptedException e) {
//            }
//            //run end ...");
//            return System.currentTimeMillis();
//        });
//
//        long time = future.get();
//        //time = " + time);
//    }
}
